/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.java.dao;

import com.java.mapbean.Sach;
import com.java.util.HibernateUtil;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 *
 * @author quannq
 */
public class SachDAO {

    private final SessionFactory sessionFactory = getSessionFactory();

    protected SessionFactory getSessionFactory() {
        try {
            return HibernateUtil.getSessionFactory();
        } catch (Exception e) {
            throw new IllegalStateException(
                    "Could not locate SessionFactory in JNDI");
        }
    }

    protected void persist(Sach transientInstance) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
            session.persist(transientInstance);
            transaction.commit();
            session.flush();
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    protected void update(Sach instance) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
            session.update(instance);
            transaction.commit();
            session.flush();
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    protected void save(Sach instance) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
            session.save(instance);
            transaction.commit();
            session.flush();
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    protected void delete(Sach persistentInstance) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
            session.delete(persistentInstance);
            transaction.commit();
            session.flush();
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    protected Sach merge(Sach detachedInstance, Session session) {
        try {
            Sach result = (Sach) session.merge(detachedInstance);
            return result;
        } catch (RuntimeException re) {
            throw re;
        }
    }

    protected Sach findById(int id) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
            Sach instance = (Sach) session.get(
                    "com.java.mapbean.Sach", id);
            transaction.commit();
            session.flush();
            return instance;
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    @SuppressWarnings("unchecked")
    protected List<Sach> findAll() {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();

            List<Sach> results = session.createQuery(
                    "from com.java.mapbean.Sach").list();

            transaction.commit();
            session.flush();
            return results;
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    protected List<Sach> findTheLoai1(int matl1) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();

            List<Sach> results = session.createQuery(
                    "from com.java.mapbean.Sach s where s.theloai1.matl1="
                    + matl1).list();

            transaction.commit();
            session.flush();
            return results;
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    protected List<Sach> findKM(int matl1, int begin) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
            String hql = "from com.java.mapbean.Sach s "
                    + "where s.giamgia>0 and s.theloai1.matl1="
                    + matl1 + " order by s.giamgia desc";

            Query query = session.createQuery(hql);
            query.setFirstResult(begin);
            query.setMaxResults(10);
            List<Sach> results = query.list();

            transaction.commit();
            session.flush();
            return results;
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    protected List<Sach> findKM(int matl1, int matl2, int begin) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
            String hql = "from com.java.mapbean.Sach s "
                    + "where s.giamgia>0 and s.theloai1.matl1="
                    + matl1 + " and s.theloai2.matl2=" + matl2
                    + " order by s.giamgia desc";

            Query query = session.createQuery(hql);
            query.setFirstResult(begin);
            query.setMaxResults(10);
            List<Sach> results = query.list();

            transaction.commit();
            session.flush();
            return results;
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    protected List<Sach> findKM(int begin) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
            String hql = "from com.java.mapbean.Sach s "
                    + "where s.giamgia>0 order by s.giamgia desc";

            Query query = session.createQuery(hql);
            query.setFirstResult(begin);
            query.setMaxResults(10);
            List<Sach> results = query.list();

            transaction.commit();
            session.flush();
            return results;
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    protected List<Sach> find4KM(int begin) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
            String hql = "from com.java.mapbean.Sach s "
                    + "where s.giamgia>0 order by s.giamgia desc";

            Query query = session.createQuery(hql);
            query.setFirstResult(begin);
            query.setMaxResults(4);
            List<Sach> results = query.list();

            transaction.commit();
            session.flush();
            return results;
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    protected List<Sach> find4KM(int matl1, int begin) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
            String hql = "from com.java.mapbean.Sach s "
                    + "where s.giamgia>0 and s.theloai1.matl1="
                    + matl1 + " order by s.giamgia desc";

            Query query = session.createQuery(hql);
            query.setFirstResult(begin);
            query.setMaxResults(4);
            List<Sach> results = query.list();

            transaction.commit();
            session.flush();
            return results;
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    protected List<Sach> find4KM(int matl1, int matl2, int begin) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
            String hql = "from com.java.mapbean.Sach s "
                    + "where s.giamgia>0 and s.theloai1.matl1="
                    + matl1 + "and s.theloai2.matl2=" + matl2
                    + " order by s.giamgia desc";

            Query query = session.createQuery(hql);
            query.setFirstResult(begin);
            query.setMaxResults(4);
            List<Sach> results = query.list();

            transaction.commit();
            session.flush();
            return results;
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    protected List<Sach> findKhac(int begin) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
            String hql = "from com.java.mapbean.Sach s "
                    + "where s.giamgia=0";

            Query query = session.createQuery(hql);
            query.setFirstResult(begin);
            query.setMaxResults(10);
            List<Sach> results = query.list();

            transaction.commit();
            session.flush();
            return results;
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    protected List<Sach> findKhac(int matl1, int begin) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
            String hql = "from com.java.mapbean.Sach s "
                    + "where s.giamgia=0 and s.theloai1.matl1=" + matl1;

            Query query = session.createQuery(hql);
            query.setFirstResult(begin);
            query.setMaxResults(10);
            List<Sach> results = query.list();

            transaction.commit();
            session.flush();
            return results;
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    protected List<Sach> findKhac(int matl1, int matl2, int begin) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
            String hql = "from com.java.mapbean.Sach s "
                    + "where s.giamgia=0 and s.theloai1.matl1=" + matl1
                    + " and s.theloai2.matl2=" + matl2;

            Query query = session.createQuery(hql);
            query.setFirstResult(begin);
            query.setMaxResults(10);
            List<Sach> results = query.list();

            transaction.commit();
            session.flush();
            return results;
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    protected List<Sach> find4Khac(int begin) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
            String hql = "from com.java.mapbean.Sach s "
                    + "where s.giamgia=0";

            Query query = session.createQuery(hql);
            query.setFirstResult(begin);
            query.setMaxResults(4);
            List<Sach> results = query.list();

            transaction.commit();
            session.flush();
            return results;
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    protected List<Sach> find4Khac(int matl1, int begin) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
            String hql = "from com.java.mapbean.Sach s "
                    + "where s.giamgia=0 and s.theloai1.matl1=" + matl1;

            Query query = session.createQuery(hql);
            query.setFirstResult(begin);
            query.setMaxResults(4);
            List<Sach> results = query.list();

            transaction.commit();
            session.flush();
            return results;
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    protected List<Sach> find4Khac(int matl1, int matl2, int begin) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
            String hql = "from com.java.mapbean.Sach s "
                    + "where s.giamgia=0 and s.theloai1.matl1=" + matl1
                    + " and s.theloai2.matl2=" + matl2;

            Query query = session.createQuery(hql);
            query.setFirstResult(begin);
            query.setMaxResults(4);
            List<Sach> results = query.list();

            transaction.commit();
            session.flush();
            return results;
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    protected int countKM() {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();

            List<Long> results = session.createQuery(
                    "select count(s.masach) from com.java.mapbean.Sach s "
                    + "where s.giamgia>0").list();

            transaction.commit();
            session.flush();
            return results.get(0).intValue();
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    protected int countKM(int matl1) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();

            List<Long> results = session.createQuery(
                    "select count(s.masach) from com.java.mapbean.Sach s "
                    + "where s.giamgia>0 and s.theloai1.matl1=" + matl1).list();

            transaction.commit();
            session.flush();
            return results.get(0).intValue();
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    protected int countKM(int matl1, int matl2) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();

            List<Long> results = session.createQuery(
                    "select count(s.masach) from com.java.mapbean.Sach s "
                    + "where s.giamgia>0 and s.theloai1.matl1=" + matl1
                    + " and s.theloai2.matl2=" + matl2).list();

            transaction.commit();
            session.flush();
            return results.get(0).intValue();
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    protected int countKhac() {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();

            List<Long> results = session.createQuery(
                    "select count(s.masach) from com.java.mapbean.Sach s"
                    + " where s.giamgia=0").list();

            transaction.commit();
            session.flush();
            return results.get(0).intValue();
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    protected int countKhac(int matl1) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();

            List<Long> results = session.createQuery(
                    "select count(s.masach) from com.java.mapbean.Sach s "
                    + "where s.giamgia=0 and s.theloai1.matl1=" + matl1).list();

            transaction.commit();
            session.flush();
            return results.get(0).intValue();
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    protected int countKhac(int matl1, int matl2) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();

            List<Long> results = session.createQuery(
                    "select count(s.masach) from com.java.mapbean.Sach s "
                    + "where s.giamgia=0 and s.theloai1.matl1=" + matl1
                    + " and s.theloai2.matl2=" + matl2).list();

            transaction.commit();
            session.flush();
            return results.get(0).intValue();
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    protected List<Sach> findAll(int matl1, int matl2, int matl3, int begin) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
            String hql = "from com.java.mapbean.Sach s "
                    + "where s.theloai1.matl1=" + matl1
                    + " and s.theloai2.matl2=" + matl2
                    + " and s.theloai3.matl3=" + matl3;

            Query query = session.createQuery(hql);
            query.setFirstResult(begin);
            query.setMaxResults(10);
            List<Sach> results = query.list();

            transaction.commit();
            session.flush();
            return results;
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    protected int count(int matl1, int matl2, int matl3) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();

            List<Long> results = session.createQuery(
                    "select count(s.masach) from com.java.mapbean.Sach s "
                    + "where s.theloai1.matl1=" + matl1
                    + " and s.theloai2.matl2=" + matl2
                    + " and s.theloai3.matl3=" + matl3).list();

            transaction.commit();
            session.flush();
            return results.get(0).intValue();
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    protected List<Sach> search(int matl1, String noidung, int begin) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
            String hql = "from com.java.mapbean.Sach s "
                    + "where s.theloai1.matl1=" + matl1
                    + " and (s.tensach like '%" + noidung + "%'"
                    + " or s.tacgia.tentacgia like '%" + noidung + "%'"
                    + " or s.nhaxuatban.tennxb like '%" + noidung + "%')";

            Query query = session.createQuery(hql);
            query.setFirstResult(begin);
            query.setMaxResults(10);
            List<Sach> results = query.list();
            transaction.commit();
            session.flush();
            return results;
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    protected int countSearch(int matl1, String noidung) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();

            List<Long> results = session.createQuery(
                    "select count(s.masach) from com.java.mapbean.Sach s "
                    + "where s.theloai1.matl1=" + matl1
                    +" and (s.tacgia.tentacgia like '%"+noidung+"%'"
                    +" or s.nhaxuatban.tennxb like '%"+noidung+"%'"
                    +" or s.tensach like '%"+noidung+"%')").list();

            transaction.commit();
            session.flush();
            return results.get(0).intValue();
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }
}
